package com.bjsxt.springmvc.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.controller.BasicController;
import com.bjsxt.common.util.ConstantFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.pojo.ABank;
import com.bjsxt.investor.pojo.ACate;
import com.bjsxt.investor.pojo.AGrant;
import com.bjsxt.investor.pojo.AInvestor;
import com.bjsxt.investor.pojo.AInvestorBank;
import com.bjsxt.investor.pojo.AInvestorCate;
import com.bjsxt.investor.service.IInvestorService;
import com.bjsxt.system.pojo.ARegion;
import com.bjsxt.system.service.IRegionService;

@Controller
@RequestMapping("/back/system/")
public class SystemBackController extends BasicController
{
	@Resource
	private IRegionService regionService;

	@Resource
	private IInvestorService investorService;

	/**
	 * 地区的Controller
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("regionList.htm")
	public String regionList(HttpServletRequest request)
	{
		/* 接受分页参数 */
		PageInfoUtil pageInfoUtil = super.getPageInfo(request);
		/* 封装接受参数 */
		Map<String, Map<String, Object>> basicMap = this.getCondMap(request);
		System.out.println("分页信息----》" + pageInfoUtil);
		/* 数据库查询 */
		/* 封装基本查询条件 */
		Map<String, Object> condMap = basicMap.get("condMap");
		/* 封装额外的查询条件 */
		String parentId = request.getParameter("parentId");
		if (parentId != null && !"".equalsIgnoreCase(parentId))
		{
			condMap.put("parentId", Integer.valueOf(parentId));
		}
		System.out.println("接受的返回参数-----》" + basicMap);
		List<ARegion> list = this.regionService.findListRegionService(pageInfoUtil, condMap);
		/* 返回数据 */
		request.setAttribute("list", list);
		request.setAttribute("parentId", parentId);
		request.setAttribute("map", basicMap.get("sourceMap"));
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/back/system/regionList";
	}

	/*========================Investor相关操作开始=================================*/
	/**
	 * 出资方的InvestorController
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("investorList.htm")
	public String investorList(HttpServletRequest request)
	{
		/* 接受数据参数 */
		/* 接受分页信息 */
		PageInfoUtil pageInfoUtil = this.getPageInfo(request);
		/* 接受查询信息 */
		Map<String, Map<String, Object>> resultMap = this.getCondMap(request);
		/* 查询的参数 */
		Map<String, Object> condMap = resultMap.get("condMap");
		/* 原参数 */
		Map<String, Object> sourceMap = resultMap.get("sourceMap");
		/* 接受其他参数 */
		String coopType = request.getParameter("coopType");
		if (coopType != null && !"".equalsIgnoreCase(coopType))
		{
			condMap.put("coopType", Integer.valueOf(coopType));
		}

		/* 查询 */
		List<AInvestor> list = this.investorService.findListInvestorService(pageInfoUtil, condMap);
		/* 将参数回传 */
		sourceMap.put("coopType", coopType);

		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("map", sourceMap);
		return "/back/system/investor/investorList";
	}

	/**
	 * 授权列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("grantList.htm")
	public String grantList(HttpServletRequest request)
	{
		/* 接受数据参数 */
		/* 接受分页信息 */
		PageInfoUtil pageInfoUtil = this.getPageInfo(request);
		/* 接受查询信息 */
		Map<String, Map<String, Object>> resultMap = this.getCondMap(request);
		/* 查询的参数 */
		Map<String, Object> condMap = resultMap.get("condMap");
		/* 原参数 */
		Map<String, Object> sourceMap = resultMap.get("sourceMap");

		/* 查询 */
		List<AGrant> list = this.investorService.findListGrantService(pageInfoUtil, condMap);
		/* 将参数回传 */

		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("map", sourceMap);
		return "/back/system/investor/grantList";
	}
	/**
	 * 权限分类的列表
	 * @param request
	 * @return
	 */
	@RequestMapping("cateList.htm")
	public String cateList(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = this.getPageInfo(request);
		Map<String, Map<String, Object>> map = this.getCondMap(request);
		Map<String,Object> sourceMap = map.get("sourceMap");
		Map<String,Object> condMap = map.get("condMap");
		/*  接受和设置其他参数  */
		String iid = request.getParameter("iid");
		String cateType = request.getParameter("cateType");
		if(!"".equalsIgnoreCase(cateType) && cateType != null)
		{
			condMap.put("cateType", Byte.valueOf(cateType));
		}
		String oper = request.getParameter("oper");
		
		sourceMap.put("iid", iid);
		sourceMap.put("oper", oper);
		
		List<ACate> list = this.investorService.findListCateService(pageInfoUtil, condMap);
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("map", sourceMap);
		return "back/system/investor/cateList";
	}
	@RequestMapping(value ="investorListCate", produces ="text/html;charset=UTF-8")
	public @ResponseBody String investorListCate(HttpServletRequest request)
	{
		/* 接受参数 */
		String iid = request.getParameter("iid");
		String[] ids = request.getParameterValues("ids");
		
		if(ids!=null && iid != null && !"".equalsIgnoreCase(iid))
		{
			int succeed = 0;
			int fail = 0;
			StringBuffer failsb = new StringBuffer("失败明细:");
			for (int i = 0; i < ids.length; i++)
			{
				String cateId = ids[i];
				AInvestorCate investorGrant = new AInvestorCate();
				investorGrant.setCateId(Integer.valueOf(cateId));
				investorGrant.setInvestorId(Integer.valueOf(iid));
				investorGrant.setStatus(Byte.valueOf("1"));
				investorGrant.setCreateTime(new Date());
				investorGrant.setPubTime(new Date());
				investorGrant.setUpdateTime(new Date());
				JSONObject jsonObject = this.investorService.insertInvestorCateService(investorGrant);
				
				if(jsonObject.get("code").equals("1"))
				{
					succeed++;
					continue;
				}
				fail++;
				failsb.append(ids[i]+",");
			}
			info = "需要插入信息:"+ids.length+"条,成功:"+succeed+"条,失败:"+fail+"条";
			if(succeed != ids.length)
			{
				info += failsb.toString();
				info =  info.substring(0, info.length()-1);
			}
			return  this.getResultJson(request, info).toJSONString();
		}
		return this.getResultJson(request, "别瞎选").toJSONString();
	}
	/**
	 * 出资方修改
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("investorUpdate.htm")
	public String investorUpdate(HttpServletRequest request, String id)
	{

		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AInvestor investor = this.investorService.findOneInvestorService(condMap);
		request.setAttribute("investor", investor);

		return "/back/system/investor/investorUpdate";
	}

	/**
	 * 出资方修改
	 * 
	 * @param request
	 * @param id
	 * @return
	 * @produces: 字符编码设置
	 */
	@RequestMapping(value = "investorUpdateSubmit.htm", produces = "text/html;charset=UTF-8")
	public @ResponseBody String investorUpdateSubmit(HttpServletRequest request, String id, MultipartFile file)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AInvestor investor = this.investorService.findOneInvestorService(condMap);
		if (investor != null)
		{
			/* 封装Investor对象 */
			investor = this.getInvestorUpdateCond(investor, request);
			investor.setPubTime(new Date());

			/* 获取头像信息 */
			String rootPath = request.getSession().getServletContext().getRealPath("/");
			condMap.put("rootPath", rootPath);
			condMap.put("file", file);
			JSONObject jsonObject = this.investorService.updateInvestorService(investor, condMap);
			info = jsonObject.get("info") + "";
		}
		return this.getResultJson(request, info).toJSONString();
	}

	/**
	 * 银行的InvestorController
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("bankList.htm")
	public String bankList(HttpServletRequest request)
	{
		/* 接受数据参数 */
		/* 接受分页信息 */
		PageInfoUtil pageInfoUtil = this.getPageInfo(request);
		/* 接受查询信息 */
		Map<String, Map<String, Object>> resultMap = this.getCondMap(request);
		/* 查询的参数 */
		Map<String, Object> condMap = resultMap.get("condMap");
		/* 原参数 */
		Map<String, Object> sourceMap = resultMap.get("sourceMap");
		/* 设置参数 */
		String iid = request.getParameter("iid");
		String oper = request.getParameter("oper");
		sourceMap.put("iid", iid);
		sourceMap.put("oper", oper);
		/* 查询 */
		List<ABank> list = this.investorService.findListBankService(pageInfoUtil, condMap);
		/* 将参数回传 */
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("map", sourceMap);
		return "/back/system/investor/bankList";
	}

	@RequestMapping(value = "bankListGrant", produces = "text/html;charset=UTF-8")
	public @ResponseBody String bankListGrant(HttpServletRequest request)
	{
		int succeed = 0;
		int fail = 0;
		String failInfo = "";
		/* 接受参数 */
		String iid = request.getParameter("iid");
		String[] ids = request.getParameterValues("ids");
		/* 循环入库 */
		if (!"".equalsIgnoreCase(iid))
		{
			for (int i = 0; i < ids.length; i++)
			{
				AInvestorBank investorBank = new AInvestorBank();
				investorBank.setInvestorId(Integer.valueOf(iid));
				investorBank.setBankId(Integer.valueOf(ids[i]));
				investorBank.setCreateTime(new Date());
				investorBank.setUpdateTime(new Date());
				investorBank.setPubTime(new Date());
				JSONObject jsonObject = this.investorService.insertInvestorBankService(investorBank);
				if ("1".equalsIgnoreCase((jsonObject.get("code") + "")))
				{
					succeed++;
					continue;
				}
				/* 入库失败 */
				fail++;
				failInfo += "," + ids[i];
			}
		}
		info = "总共的需要添加" + ids.length + "条," + "成功：" + succeed + ",失败：" + fail;
		if (fail > 0)
		{
			info += ",失败明细" + failInfo;
		}
		JSONObject resultJson = this.getResultJson(request, info);
		return resultJson.toJSONString();
	}
	@RequestMapping("investorDetail.htm")
	public String investorDetail(HttpServletRequest request,String id)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", Integer.valueOf(id));
		AInvestor investor = this.investorService.findOneInvestorService(condMap );
		/* 获得的授权分类 */
		for (Iterator iterator = investor.getInvestorCateSet().iterator(); iterator.hasNext();)
		{
			AInvestorCate investorCate = (AInvestorCate) iterator.next();
			/* 获得赞助商对应的授权分类的Id */
			condMap.clear();
			condMap.put("id", investorCate.getCateId());
			/* 查出分类内容 */
			ACate cate = this.investorService.findOneCateService(condMap);
			investorCate.setCate(cate);
			/* 查出分类对应的授权内容 */
			condMap.clear();
			condMap.put("id", cate.getId());
			List<AGrant> listGrant = this.investorService.findListGrantService(null, condMap);
			
		}
		/* 获得的银行分类 */
		for (Iterator iterator = investor.getInvestorBankSet().iterator(); iterator.hasNext();)
		{
			AInvestorBank investorBank = (AInvestorBank) iterator.next();
			System.out.println(investorBank.getBank().getName());
		}
		
		/*  */
		return "";
	}
	/*========================Investor相关操作开始=================================*/
	/* ==========================辅助方法 ====================== */

	private AInvestor getInvestorUpdateCond(AInvestor investor, HttpServletRequest request)
	{
		String code = request.getParameter("code");
		String url = request.getParameter("url");
		String contactName = request.getParameter("contactName");
		String contactPhone = request.getParameter("contactPhone");
		String totalNum = request.getParameter("totalNum");
		String succedNum = request.getParameter("succedNum");
		String coopType = request.getParameter("coopType");
		String status = request.getParameter("status");
		String content = request.getParameter("content");
		String name = request.getParameter("name");

		investor.setCode(code);
		investor.setName(name);
		investor.setContactName(contactName);
		investor.setContactPhone(contactPhone);
		investor.setContent(content);
		investor.setCoopType(Byte.valueOf(coopType));
		investor.setStatus(Byte.valueOf(status));
		investor.setUrl(url);
		investor.setTotalNum(Integer.valueOf(totalNum));
		investor.setSuccedNum(Integer.valueOf(succedNum));

		return investor;
	}
}
